package com.huahan.auth.granter;

import cn.hutool.core.collection.CollectionUtil;
import com.huahan.auth.domain.UserInfo;
import com.huahan.auth.exception.BusinessException;
import com.huahan.auth.mapper.UserInfoMapper;
import com.huahan.auth.security.component.HuahanUserDetail;
import com.mall.common.constant.CommonConstant;
import com.mall.common.constant.LoginTypeEnum;
import com.mall.common.constant.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.*;

/**
 * @author wj
 * @Date
 */
public class DDTokenGranter extends AbstractTokenGranter {
    /***************************************************************
     *                                                             *
     * ***********************4-钉钉授权************************
     *                                                             *
     * *************************************************************/
    private UserInfoMapper userInfoDao;
    public DDTokenGranter(AuthorizationServerTokenServices tokenServices,
                          ClientDetailsService clientDetailsService,
                          OAuth2RequestFactory requestFactory) {
        super(tokenServices, clientDetailsService, requestFactory, LoginTypeEnum.DD_CODE.getValue());
    }

    public void setAccountService(UserInfoMapper userInfoDao) {
        this.userInfoDao = userInfoDao;
    }

    @Override
    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client,
                                                           TokenRequest tokenRequest) {
        Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
        List<GrantedAuthority> grantedAuthorities = new ArrayList();
        grantedAuthorities.add(new SimpleGrantedAuthority("USER"));
        // 客户端提交的手机号码
        String phoneNumber = parameters.get(CommonConstant.DEFAULT_PARAMETER_NAME_PHONE);
        if (StringUtils.isBlank(phoneNumber)) {
            throw new BusinessException(ResultCode.PHONE_NUMBER_IS_EMPTY);
        }
        // 客户端提交的验证码
        String unionId = parameters.get(CommonConstant.DEFAULT_PARAMETER_NAME_DD_UNIONID);
        if (StringUtils.isBlank(unionId)) {
            throw new BusinessException(ResultCode.UN_KNOWN_ERROR);
        }
        Map<String, Object> map = new HashMap<>();
        // 客户端提交的手机号码 根据手机号码查询用户
        map.put("phone_number", phoneNumber);
        map.put("is_app", "1");
        map.put("dd_union_id", unionId);
        List<UserInfo> list = userInfoDao.selectByMap(map);
        UserInfo user =new UserInfo();
        if(CollectionUtil.isEmpty(list)){
            user= list.get(0);
        }
        HuahanUserDetail userDetails = new HuahanUserDetail(user,grantedAuthorities);
        // 验证用户状态
        AbstractAuthenticationToken userAuth = new UsernamePasswordAuthenticationToken(user, null, userDetails.getAuthorities());
        userAuth.setDetails(parameters);
        OAuth2Request oAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);

        return new OAuth2Authentication(oAuth2Request, userAuth);

    }
}
